
package br.com.almanach.persistencia.cadastro;

import br.com.almanach.dominio.cadastro.Musica;
import java.util.List;
import javax.ejb.Stateless;
import javax.ejb.LocalBean;

import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;

@Stateless
@LocalBean
public class MusicaPersistencia {

    @PersistenceContext
    private EntityManager entityManager;

    public List<Musica> buscar(String nome, String artista, String album, int registro , int quantidade){
        try{
            CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
            CriteriaQuery criteriaQuery = criteriaBuilder.createQuery();
            Root<Musica> musica = criteriaQuery.from(Musica.class);
            criteriaQuery.select(musica);

            if(nome != null && artista != null && album != null){
                criteriaQuery.where(
                        criteriaBuilder.and(criteriaBuilder.like(musica.<String>get("nome"), nome+"%"),
                        criteriaBuilder.like(musica.<String>get("artista"), artista+"%"),
                        criteriaBuilder.like(musica.<String>get("album"), album+"%")
                    ));
            }else if(nome != null && artista != null && album == null){
                criteriaQuery.where(
                        criteriaBuilder.and(criteriaBuilder.like(musica.<String>get("nome"), nome+"%"),
                        criteriaBuilder.like(musica.<String>get("artista"), artista+"%")
                    ));
            }else if(nome != null && artista == null && album != null){
                criteriaQuery.where(
                        criteriaBuilder.and(criteriaBuilder.like(musica.<String>get("nome"), nome+"%"),
                        criteriaBuilder.like(musica.<String>get("album"), album+"%")
                    ));
            }else if(nome != null && artista == null && album == null){
                criteriaQuery.where(criteriaBuilder.like(musica.<String>get("nome"), nome+"%"));
            }else if(nome == null && artista != null && album != null){
                criteriaQuery.where(
                        criteriaBuilder.and( criteriaBuilder.like(musica.<String>get("artista"), artista+"%"),
                        criteriaBuilder.like(musica.<String>get("album"), album+"%")
                    ));
            }else if(nome == null && artista != null && album == null){
                criteriaQuery.where(criteriaBuilder.like(musica.<String>get("artista"), artista+"%"));
            }else if(nome == null && artista == null && album != null){
                criteriaQuery.where(criteriaBuilder.like(musica.<String>get("album"), album+"%"));
            }
            
            return entityManager.createQuery(criteriaQuery).setFirstResult(registro).setMaxResults(quantidade).getResultList();
            
        }catch( NoResultException e ){
            return null;
        }
    }

    public int buscar(String nome, String artista, String album){
        Number registros = null;
        try{
            CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
            CriteriaQuery criteriaQuery = criteriaBuilder.createQuery();
            Root<Musica> musica = criteriaQuery.from(Musica.class);
            criteriaQuery.select(musica);
            
            if(nome != null && artista != null && album != null){
                criteriaQuery.where(
                        criteriaBuilder.and(criteriaBuilder.like(musica.<String>get("nome"), nome+"%"),
                        criteriaBuilder.like(musica.<String>get("artista"), artista+"%"),
                        criteriaBuilder.like(musica.<String>get("album"), album+"%")
                    ));
            }else if(nome != null && artista != null && album == null){
                criteriaQuery.where(
                        criteriaBuilder.and(criteriaBuilder.like(musica.<String>get("nome"), nome+"%"),
                        criteriaBuilder.like(musica.<String>get("artista"), artista+"%")
                    ));
            }else if(nome != null && artista == null && album != null){
                criteriaQuery.where(
                        criteriaBuilder.and(criteriaBuilder.like(musica.<String>get("nome"), nome+"%"),
                        criteriaBuilder.like(musica.<String>get("album"), album+"%")
                    ));
            }else if(nome != null && artista == null && album == null){
                criteriaQuery.where(criteriaBuilder.like(musica.<String>get("nome"), nome+"%"));
            }else if(nome == null && artista != null && album != null){
                criteriaQuery.where(
                        criteriaBuilder.and( criteriaBuilder.like(musica.<String>get("artista"), artista+"%"),
                        criteriaBuilder.like(musica.<String>get("album"), album+"%")
                    ));
            }else if(nome == null && artista != null && album == null){
                criteriaQuery.where(criteriaBuilder.like(musica.<String>get("artista"), artista+"%"));
            }else if(nome == null && artista == null && album != null){
                criteriaQuery.where(criteriaBuilder.like(musica.<String>get("album"), album+"%"));
            }
            
            criteriaQuery.select(criteriaBuilder.count(musica));
            registros = (Number) entityManager.createQuery(criteriaQuery).getSingleResult();
            return registros == null ? 0 : registros.intValue();
        }catch( NoResultException e ){
            return 0;
        }
    }

    public List<Musica> buscaPorNome(String nome){
        try{
            CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
            CriteriaQuery criteriaQuery = criteriaBuilder.createQuery();
            Root<Musica> musica = criteriaQuery.from(Musica.class);
            criteriaQuery.select(musica);
            criteriaQuery.where(criteriaBuilder.like(musica.<String>get("nome"), nome+"%"));
            return entityManager.createQuery(criteriaQuery).getResultList();
        }catch( NoResultException e ){
            return null;
        }
    }

    public Musica buscar(Integer id){
        return entityManager.find(Musica.class,id);
    }

    public void incluir(Musica musica){
        entityManager.persist(musica);
    }

    public void alterar(Musica musica){
        entityManager.merge(musica);
    }

    public void remover(Musica musica){
        entityManager.remove(entityManager.find(Musica.class,musica.getId()));
    }

}